<template>
  <div>
    <el-dialog title="新增成员" :visible.sync="dialog" direction="rtl" ref="drawer" width="25%" height="25%"
      :close-on-click-modal="false">
      <el-form ref="empForm" :model="empForm" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="nickname">
          <el-input v-model="empForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="	手机号" prop="cellphone">
          <el-input v-model="empForm.cellphone"></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input v-model="empForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type='password' v-model="empForm.password"></el-input>
        </el-form-item>
        <div style="text-align: center; margin-bottom: 0;">
          <el-button type="primary" @click="submitForm">确认</el-button>
          <el-button @click="dialog = false">取消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import {
    validateCellphone
  } from '@/utils/validate'
  import {
    empAddUser
  } from '@/api/emp.js'
  import {
    md5
  } from '@/utils/MD5.js'

  export default {
    data() {
      const validCellphone = (rule, value, callback) => {
        if (!validateCellphone(value)) {
          callback(new Error('请输入正确的手机号吗'))
        } else {
          callback()
        }
      }
      return {
        dialog: false,
        empForm: {
          nickname: '',
          cellphone: '',
          username: '',
          password: '',
        },
        rules: {
          nickname: [{
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }],
          cellphone: [{
            required: true,
            trigger: 'blur',
            validator: validCellphone
          }],
          username: [{
            required: true,
            message: '请输入账号',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {

      async addUser() {
        var params = {
          nickname: this.empForm.nickname,
          cellphone: this.empForm.cellphone,
          username: this.empForm.username,
          password: md5(this.empForm.password)
        };
        var res = await empAddUser(params)
        this.close()
      },
      open() {
        this.dialog = true;
        this.resetForm();
      },

      close() {
        this.dialog = false;
        this.$parent.searchTable();
      },

      submitForm() {
        this.$refs.empForm.validate(valid => {
          if (!valid) return
          this.addUser();
        })
      },

      resetForm() {
        if (this.$refs.empForm !== undefined) {
          this.$refs.empForm.resetFields();
        }
      }
    }
  }
</script>

<style scoped lang="scss">
  .QRCodeDownload {
    padding: 15px;
  }

  // 列表字段居中
  /deep/.el-table th>.cell {
    text-align: center;
  }

  /deep/.el-table .cell {
    text-align: center;
  }
</style>
